package ${package}.utils;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import com.ishansong.common.json.Jsoner;
import com.ishansong.common.util.Networks;
import com.ishansong.log.Sensors;

import java.util.Map;

/**
 * <pre>
 * 功能描述：
 * 神策数据埋点
 *
 * </pre>
 * Author: sid
 * DateTime: 2017-11-28 下午16:08
 */
public class SensorLogs {

    private static final String hostName = Networks.getHostName();
    private static final String ip = Networks.getSiteIp();

    /**
     * 统一添加默认属性
     *
     * @param params    需要序列化的Map对象
     */
    private static String toJsonStringWithDefault(Map<String, Object> params){
        params.put("time", System.currentTimeMillis());
        params.put("hostName", hostName);
        params.put("ip", ip);
        return Jsoner.DEFAULT.toJson(params);
    }

    /**
     * 记录订单提交事件
     *
     * @param mobile       手机号
     * @param platformType 平台类型
     * @param couponType   优惠券类型
     * @param couponAmount 优惠券金额
     * @param orderId      订单编号
     * @param userId       用户编号
     */
    public static void submitOrder(String mobile, String platformType, Long couponType, Integer couponAmount, Long orderId, Long userId, String orderingSource) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("uniqueId", mobile);
        params.put("event", "SubmitOrder");

        //properties
        params.put("Platform", platformType);
        params.put("CouponType", couponType);
        params.put("CouponAmount", couponAmount);
        params.put("OrderId", orderId);
        params.put("UserId", userId);
        params.put("OrderingSource", orderingSource);

        Sensors.log(toJsonStringWithDefault(params));
    }

    /**
     * 记录订单支付事件
     *
     * @param mobile  手机号
     * @param orderId 订单编号
     */
    public static void payOrder(String mobile, Long orderId) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("uniqueId", mobile);
        params.put("event", "PayOrder");

        //properties
        params.put("OrderId", orderId);

        Sensors.log(toJsonStringWithDefault(params));
    }


    /**
     * 记录订单取消事件
     *
     * @param orderId 订单编号
     */
    public static void abortOrder(Long orderId, Integer type, String operator, String reason) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("event", "AbortOrder");

        //properties
        params.put("OrderId", orderId);
        params.put("Type", type);
        params.put("Operator", operator);
        params.put("Reason", reason);

        Sensors.log(toJsonStringWithDefault(params));
    }

    /**
     * 记录订单完成事件
     *
     * @param orderId 订单编号
     */
    public static void finishOrder(Long orderId) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("event", "FinishOrder");

        //properties
        params.put("OrderId", orderId);

        Sensors.log(toJsonStringWithDefault(params));
    }

    /**
     * 取件事件记录
     */
    public static void courierPickUp(Long courierId, Long orderId){
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("uniqueId",courierId);
        params.put("event", "CourierPickup");
        //properties
        params.put("orderId", orderId);

        Sensors.log(toJsonStringWithDefault(params));
    }

    /**
     * 记录调整积分
     *
     */
    public static void courierAdjustGrade(Map<String,Object> map) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("uniqueId", map.get("courierId"));
        params.put("event", "CourierAdjustGrade");
        //properties
        params.put("opearId",map.get("opearId"));
        if(Objects.equal(map.get("result"),1)){
            params.put("result","调整积分");
        } else {
            params.put("result","确认积分");
        }
        params.put("orderNumber",map.get("orderNumber"));

        Sensors.log(toJsonStringWithDefault(params));
    }

    public static void virtualMobile(Long courierId, Long userId, String courierMobile, String userMobile, String virtualMobile, String type, String channel, String status, String reason) {
        Map<String, Object> params = Maps.newHashMap();
        //common
        params.put("uniqueId", courierId + "");
        params.put("event", "CourierVirtualMobile");

        //properties
        params.put("courierId", courierId);
        params.put("userId", userId);
        params.put("courierMobile", courierMobile);
        params.put("userMobile", userMobile);
        params.put("virtualMobile", virtualMobile);
        params.put("type", type);
        params.put("channel", channel);
        params.put("status", status);
        params.put("reason", reason);

        Sensors.log(toJsonStringWithDefault(params));
    }

}


